<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var majorityElement = function (nums) {
        let mapping = new Map();
        nums.forEach((item) => {
          if (mapping.has(item)) {
            let curr = mapping.get(item);
            mapping.set(item, ++curr);
          } else {
            mapping.set(item, (value = 1));
          }
        });
        let maxValue = -Infinity;
        let maxKey = null;
        for (let item of mapping) {
          if (item[1] > maxValue) {
            maxValue = item[1];
            maxKey = item[0];
          }
        }
        return maxKey;
      };
      console.log(majorityElement([3, 2, 3,2,2]));
    </script>
  </body>
</html>
